<?php

namespace App\Http\Controllers\Web\Member\Seller;

use App\Http\Controllers\Web\BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cookie;

class ItemsInformationController extends BaseController
{
    protected $api_url = [
        'index' => ['get', 'seller/items'],
        'show' => ['get', 'seller/items/%d'],
        'store' => ['post', 'seller/items'],
        'item_shop' => ['get', 'seller/items/item_shop'],
        'seller_shop' => ['get', 'seller/items/seller_shop/%d'],
        'sms_notify'=> ['get', 'senditemsmessage'],
        'destroy'=>['delete', 'seller/items/%d'],
        'items_stock' => ['get', 'seller/items/stock'],
    ];

    protected $api_host;

    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->api_host = config('url.api_web');
    }

    public function index()
    {
        //参数部分
        $page = $this->request->get('page', 1);
        $params = [
            'page' => $page,
            'orderBy'=>'id',
            'sortedBy'=>'desc',
        ];

        //请求部分
        $ret = http_web($this->api_url['index'])->send($params);

        //结果处理部分
        $data = paginate($this->request, $ret['data']);

        //回填搜索框值

        return view('web.user.seller.matching', $data);
    }

    public function show($id)
    {
        $ret = http_web($this->api_url['show'],[$id])->send();
        return view('web.user.seller.matching_eidt', $ret['data']);

    }

    public function create(){
        if($this->request->cookie('items_data_arr')){
            $items_data_arr=$this->request->cookie('items_data_arr');
            $items_data_arr=unserialize($items_data_arr);
        }else{
            $items_data_arr['data']=[];
        }
        $ret = http_web($this->api_url['seller_shop'],[session('user_id')])->send();
        return view('web.user.seller.matching_add',['data'=>$ret['data'][0],'item_clone'=>$items_data_arr['data']]);
    }

    public function store(){

        $this->validate($this->request, [
            'buyer_phone' => 'required',
            'buyer_contact' => 'required',
//            'buyer_company' => 'required',
            'quote_contain' => 'required',
            'seller_phone' => 'required',
            'seller_contact' => 'required',
            'seller_company' => 'required',
        ]);
        $data=$this->request->all();
        $ret = http_web($this->api_url['store'])->send($data);

        if(isset($ret['code']) && $ret['code']===0 ){
            Cookie::queue('items_data_arr', null , -1); // 销毁
            if(isset($data['sms_notify']) && $data['sms_notify']==1){//短信通知
                $sms=[
                    'phone'=>$data['buyer_phone']
                ];
                $sms_ret = http_web($this->api_url['sms_notify'])->send($sms);

                if($sms_ret['code']==0){
                    return response()->json(['code'=>0,'message'=>'生成配单成功,短信发送成功']);
                }else{
                    return response()->json(['code'=>1,'message'=>'生成配单成功，短信发送失败']);
                }
            }
           return response()->json(['code'=>0,'message'=>'生成配单成功']);
        }else{
           return response()->json(['code'=>1,'message'=>'生成配单失败']);
        }
    }

    public function edit($id)
    {

    }

    public function update($id)
    {
        $this->validate($this->request, [
            'buyer_phone' => 'required',
            'buyer_contact' => 'required',
            'buyer_company' => 'required',
            'buyer_id' => 'required',
            'quote_contain' => 'required',
            'quote_trade_time' => 'required',
            'project_addr' => 'required',
            'seller_phone' => 'required',
            'seller_contact' => 'required',
            'seller_company' => 'required',
            'seller_id' => 'required',
            'goods_total' => 'required',
            'other_total' => 'required',
        ]);
        $data = $this->request->all([]);

        $ret = http_web($this->api_url['update'],[$id])->send($data);

        if(isset($ret['code']) && $ret['code']===0 ){
            return response()->json(['code'=>0,'message'=>'']);
        }else{
            return response()->json(['code'=>1,'message'=>$ret['message']]);
        }

    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * 关闭配单
     */
    public function destroy($id)
    {
        $ret = http_web($this->api_url['destroy'],[$id])->send();
        if($ret['code']==0){
            return response()->json(['code'=>0,'message'=>$ret['message'],'data'=>$ret['data']]);
        }else{
            return response()->json(['code'=>1,'message'=>$ret['message']]);

        }
    }

    /**
     * 配单获取用户公司名和姓名
     */
    public function item_shop(){
        $data=$this->request->all();
        $ret = http_web($this->api_url['item_shop'])->send($data);
        if($ret && isset($ret['data'][0])){
            if(empty($ret['data'][0]['company'])){
                $ret['data'][0]['buyer_company']='';
            }else{
                $ret['data'][0]['buyer_company']=$ret['data'][0]['company']['company_name'];
            }
            return response()->json(['code'=>0,'message'=>'','data'=>$ret['data'][0]]);
        }else{
            return response()->json(['code'=>1,'message'=>'手机号匹配失败']);
        }

    }


    /**
     * 复制配单
     */
    public function item_clone($id){

        $ret = http_web($this->api_url['show'],[$id])->send();
        $cookie_data=serialize($ret['data']);
        Cookie::queue('items_data_arr',$cookie_data);

        $destroy = http_web($this->api_url['destroy'],[$id])->send();//关闭配单

        if($destroy['code']==0){
           return response()->json(['code'=>0,'message'=>$destroy['message']]);
        }else{
           return response()->json(['code'=>1,'message'=>$destroy['message']]);
        }
    }

    /**
     * 配单产品
     * @return \Illuminate\Http\JsonResponse
     */
    public function items_stock(){

        $data=$this->request->all(['product_name']);
        $params = [
           'search' => search_params($data),
           'searchJoin' => 'and',
           'orderBy'=>'id',
           'sortedBy'=>'desc',
       ];

        $ret = http_web($this->api_url['items_stock'])->send($params);
        if(!empty($ret['data'])){
            foreach($ret['data'] as $k=>$v){
                $ret['data'][$k]['brand_name']=$v['brand'];
            }
        }
        if($ret['code']==0){
           return response()->json(['code'=>0,'message'=>$ret['message'],'data'=>$ret['data']]);
        }else{
           return response()->json(['code'=>1,'message'=>$ret['message']]);
        }
    }

}